<?php


namespace app\system\controller;

class AdminBase extends \app\common\Controller
{
	public $admin_info = [];
	public $page_no = 1;
	public $page_size = 10;
	public $page_size_max = 1.0E+18;
	public $api_log_id;
	public $request_times = 0;
	public $request_time = 0;
	public function __construct()
	{
		$this->request_times = microtime(true);
		$this->request_time = time();
		parent::__construct();
		if (input("page") != "") {
			$this->page_no = input("page");
		}
		if (input("limit") != "") {
			$this->page_size = input("limit");
		}
		if ($this->page_size == -1) {
			$this->page_size = $this->page_size_max;
		}
		$system_admin_token = \think\facade\Request::header("system-admin-token");
		$system_admin_id = \think\facade\Request::header("system-admin-id");
		if (!$system_admin_id || !$system_admin_token) {
			$this->response(\app\system\model\Code::loginFailure, "缺少登录身份，请先登录");
		}
		$admin_info = \think\Db::name("system_admin")->where(["id" => $system_admin_id])->find();
		if (empty($admin_info) || $admin_info["token"] != $system_admin_token) {
			$this->response(\app\system\model\Code::loginFailure, "登录失效已在别处登录,请重新登录");
		}
		$token_info = json_decode(hex2bin($system_admin_token), true);
		if ((!is_array($token_info) || !isset($token_info["ip"]) || $token_info["ip"] != \request()->ip()) && $admin_info["verify_login_ip"] === 1) {
			$this->response(\app\system\model\Code::loginFailure, "登录IP发生变化,请重新登录(当前ip:" . \request()->ip() . "，若不想开启此验证可在后台操作员管理设置关闭)");
		}
		if (!is_array($token_info) || !isset($token_info["startTime"]) || time() - $token_info["startTime"] > 86400) {
			$this->response(\app\system\model\Code::loginFailure, "登录状态过期,请重新登录");
		}
		if (!defined("system_already_authentication")) {
			define("system_already_authentication", time());
			$system = new \app\common\system\System();
			$system->check_auth();
			$isSystemAuthUnpaid = isSystemAuthUnpaid($system);
			if (isSystemAuthUnpaidAndNewHaveVersionUpdate($system)) {
				$this->response(\app\system\model\Code::fail, "当前授权为基础版,检测到有新版本更新请在 系统管理->关于系统 里面更新到最新版本" . \app\common\model\DefaultConfig::SystemAuthUnpaidSuffixText);
			}
			$controller_action = parseUnderline(request()->controller()) . "/" . request()->action();
			$this->admin_info = $admin_info;
			session("system_admin_info", $admin_info);
			$operation = 2;
			$CheckPermissions = \think\Db::name("system_admin_role")->alias("role")->join("system_admin_menu menu", "menu.uri='{$controller_action}' and role.id = {$admin_info["role_id"]}")->join("system_admin_role_auth role_auth", "role.id=role_auth.role_id and menu.id=role_auth.menu_auth_id and menu.disable=0  and menu.type={$operation}")->select();
			$auth_white_list = ["index/get_self_info", "index/get_index_data", "index/getmenu", "index/getauth", "logs/getlist"];
			if (!$CheckPermissions && $admin_info["root"] != 1 && !in_array($controller_action, $auth_white_list)) {
				$this->response(\app\system\model\Code::fail, "您没有权限");
			}
			if (in_array($controller_action, \app\common\model\DefaultConfig::test_site_system_black_list)) {
				if (\app\common\server\ConfigServer::get("system", \app\common\model\DefaultConfig::SystemTestDemoField, false)) {
					$this->response(\app\system\model\Code::fail, "当前站点为演示站点,暂不支持本操作" . \app\common\model\DefaultConfig::SystemTestDemoTipsSuffixText);
				}
			}
			if (in_array($controller_action, \app\common\model\FreeQuota::SystemAuthUnpaidBlackList) && $isSystemAuthUnpaid) {
				$this->response(\app\system\model\Code::fail, "当前授权为基础版,暂不支持本操作" . \app\common\model\DefaultConfig::SystemAuthUnpaidSuffixText);
			}
			$logs_white_list = ["index/get_self_info", "index/getmenu", "index/getauth", "logs/getlist", "app/getlist", "app/get_classify_list"];
			if (!in_array($controller_action, $logs_white_list)) {
				$this->add_system_admin_log($controller_action);
			}
		}
	}
	public function add_system_admin_log($uri)
	{
		$admin_info = $this->admin_info;
		$data = ["admin_id" => $admin_info["id"], "account" => $admin_info["account"], "name" => $admin_info["name"], "create_time" => time(), "uri" => $uri, "type" => request()->isPost() ? "POST" : "GET", "param" => json_encode(request()->param(), JSON_UNESCAPED_UNICODE), "ip" => request()->ip()];
		$api_log_id = \think\Db::name("system_admin_log")->insertGetId($data);
		$this->api_log_id = $api_log_id;
	}
	public function response($code, $msg, $data = [], ...$other)
	{
		if ($code != \app\system\model\Code::success) {
			\think\Db::rollback();
		}
		if ($this->api_log_id) {
			$response_times = microtime(true);
			\think\Db::name("system_admin_log")->where(["id" => $this->api_log_id])->update(["response_code" => $code, "response_time" => time(), "create_time" => $this->request_time, "response_consuming_time" => round($response_times - $this->request_times, 5)]);
		}
		$response = ["code" => $code, "msg" => $msg, "data" => $data];
		foreach ($other as $array) {
			$response[$array[0]] = $array[1];
		}
		echo json_encode($response, JSON_UNESCAPED_UNICODE);
		exit;
	}
}